import requests
from cachetools import cached, TTLCache
from utils.anti_spider import AntiSpiderMiddleware

class SmartParser:
    def __init__(self):
        self.middleware = AntiSpiderMiddleware()
        self.cache = TTLCache(maxsize=100, ttl=3600)

    @cached(cache)
    def parse(self, url):
        try:
            html = self.middleware.safe_request(url)
            adapter = BaseAdapter.factory(url)
            return adapter.parse(html)
        except Exception as e:
            self.log_error(e)
            return None